<?php
/**
 * 处理swoole服务端回调数据处理
 */
namespace Home\Controller;
use Think\Controller;
class SwooleClientController extends Controller {
    public function index(){
        echo "hello world";
    }

    /**
     * @param $game     小程序
     * @param $action   操作行为
     * @param $test     是否是测试用
     * @param call_back 回调链接
     */
    public function api_accept()
    {
        $game       =   I('post.game','','htmlspecialchars');
        $action     =   I('post.action','','htmlspecialchars');
        $test       =   I('post.test','','htmlspecialchars');
        $call_back  =   I('post.call_back','','htmlspecialchars');
        $table      =   I('post.table','','htmlspecialchars');

        if(! empty($game) && ! empty($action) && ! empty($call_back) && ! empty($table))
        {
            if (! empty($test))
            {
               self::test_deal($game,$table);
            }
            else
            {
                self::do_deal($game,$table);
            }
        }

        echo '欢迎SwooleClient';
    }

    /**
     * @param $game
     * @param $table
     */
    private function test_deal($game,$table)
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1',6379);

        //清空限制表
        $redis->set($game.'_member','');

        //处理已经发送的数据
        //'update_'.$data['game'];

        //失败数据
        //$fail_game	=	'fail_message_'.$postdata['game'];

        //清理数据 update_worldcup
        $update_message     =   'update_'.$game;
        $id_data            =   array();

        while(true){
            try{
                $value = $redis->lPop($update_message);
                if(!$value){
                    break;
                }

                //投递异步任务
                $id_data[]  =   $value;

            }catch(Exception $e){
                echo $e->getMessage();
            }
        }

        //删除数据
        $del_status     =   '删除不成功';
        if (! empty($id_data))
        {
            $del_data   =   implode(",",$id_data);
            $result     =   M($table)->delete($del_data); // 删除主键为1,2和5的用户数据

            if ($result)
            {
                $del_status     =   '删除成功';
            }
        }

        $log               =    array();
        $log['module']     =    'SwooleClient';
        $log['table']      =    $table;
        $log['ids']        =    implode(",",$id_data);
        $log['game']       =    $game;
        $log['url']        =    $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
        $log['log']        =    $del_status.json_encode($_POST).'完成一次swoole推送以及更新'.count($id_data);
        $log['create_time']=    time();
        M('swoole_log')->add($log);
    }

    /**
     * @param $game
     * @param $table
     */
    private function do_deal($game,$table)
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1',6379);

        //清空限制表
        $redis->set($game.'_member','');

        //处理已经发送的数据
        //'update_'.$data['game'];

        //失败数据
        //$fail_game	=	'fail_message_'.$postdata['game'];

        //清理数据 update_worldcup
        $update_message     =   'update_'.$game;
        $id_data            =   array();

        while(true){
            try{
                $value = $redis->lPop($update_message);
                if(!$value){
                    break;
                }

                //投递异步任务
                $id_data[]  =   $value;

            }catch(Exception $e){
                echo $e->getMessage();
            }
        }

        //删除数据
        $del_status     =   '删除不成功';
        if (! empty($id_data))
        {
            $del_data   =   implode(",",$id_data);
            $result     =   M($table)->delete($del_data); // 删除主键为1,2和5的用户数据

            if ($result)
            {
                $del_status     =   '删除成功';
            }
        }

        $log               =    array();
        $log['module']     =    'SwooleClient';
        $log['table']      =    $table;
        $log['ids']        =    implode(",",$id_data);
        $log['game']       =    $game;
        $log['url']        =    $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
        $log['log']        =    $del_status.json_encode($_POST).'完成一次swoole推送以及更新'.count($id_data);
        $log['create_time']=    time();
        M('swoole_log')->add($log);
    }
}